package com.xone.collaboration.service.impl;

import com.xone.collaboration.service.PushService;
import com.xone.collaboration.common.Result;
import com.xone.collaboration.common.ResultUtils;
import com.xone.push.center.auth.AuthUtils;
import com.xone.push.center.client.PushClient;
import com.xone.push.center.client.PushMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.sound.midi.Track;

@Component
@Slf4j
public class PushServiceImpl implements PushService {

    @Autowired
    private PushClient pushClient;

    @Override
    public Result<String> getToken(String appId, String topic) {
        try{
            Assert.notNull(appId,"appId can't be null");
            Assert.notNull(appId,"topic can't be null");
            String token =  AuthUtils.getToken(appId,topic) ;
            return ResultUtils.success(token);
        }catch (Exception e){
            log.error("get token error:",e);
            return ResultUtils.fail(e.getMessage());
        }
    }

    @Override
    public Boolean sendMessage(PushMessage message) {
        log.info("send message start:{}",message);
        try{
            pushClient.send(message);
        }catch (Exception e){
            log.error("send message error:",e);
            return false;
        }
        return true;
    }
}
